package org.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.User;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.auth.JwtAuth;
import org.example.mapper.UserMapper;
import org.example.service.LoginService;
import org.example.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private User user;

    /**
     * API1 登录，给用户名和密码，返回所有的属性（除了密码）
     * 备注：后端用了MD5加密
     */
    public CommonResponse<User> login(
            String UserName,
            String Password,
            String UseType
    ){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UserName",UserName);
        String encryptPassword = MD5.encrypt(Password);
        queryWrapper.eq("Password",encryptPassword);
        queryWrapper.eq("UseType",UseType);
        User user1 = userMapper.selectOne(queryWrapper);
        String token = JwtAuth.jwtEncipher(UserName,UserName);
        if (user1 != null){
            user1.setPassword(null);
            return CommonResponse.createForSuccess(token,user1);
        }else{
            return CommonResponse.createForError("用户信息查找失败");
        }
    }
}
